package liujinliang.natappvip.cc.componentdiv;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import liujinliang.natappvip.cc.componentdiv.view.ImageBannerViewGroup;

public class MainActivity extends AppCompatActivity {

    // initialize imageBannerViewGroup
    ImageBannerViewGroup imageBannerViewGroup;

    // initialize images array source
    int[] images = {R.drawable.test_a, R.drawable.test_b, R.drawable.test_c};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Set up the imageBannerViewGroup
        imageBannerViewGroup = (ImageBannerViewGroup) findViewById(R.id.image_banner);

        // Set up the click listener
        imageBannerViewGroup.setImageBannerClickListener(new ImageBannerViewGroup.ImageBannerClickListener() {
            @Override
            public void clickListener(int pos) {
                Toast.makeText(MainActivity.this, "You Clicked Image" + pos, Toast.LENGTH_SHORT).show();
            }
        });

        // calculate the width of phone
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        int width = dm.widthPixels;

        // filling imageBannerViewGroup with images
        for (int image : images) {
            ImageView imageView = new ImageView(this);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setLayoutParams(new ViewGroup.LayoutParams(width, ViewGroup.LayoutParams.WRAP_CONTENT));
            imageView.setImageResource(image);
            imageBannerViewGroup.addView(imageView);
        }
    }
}
